package com.whitedust.ltzf.test;

import com.alibaba.fastjson.JSON;
import com.whitedust.ltzf.factory.Configuration;
import com.whitedust.ltzf.factory.IPayFactory;
import com.whitedust.ltzf.factory.defaults.DefaultPayFactory;
import com.whitedust.ltzf.payments.nativepay.model.*;
import com.whitedust.ltzf.payments.nativepay.NativePayApiService;
import com.whitedust.ltzf.payments.nativepay.model.PrepayRequest;
import com.whitedust.ltzf.payments.nativepay.model.PrepayResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * @author A
 * @since 2025/7/25 14:36
 */
@Slf4j
public class NativePayApiServiceTest {
    private NativePayApiService nativePayApiService;
    @Before
    public void init(){
        Configuration configuration = new Configuration("1104268","1673424392","6d3e889f359fcb83d150e9553a9217b9");
        IPayFactory payFactory = new DefaultPayFactory(configuration);
        this.nativePayApiService = payFactory.nativePayApiService();
    }
    @Test
    public void test_pay() throws IOException {
        PrepayRequest request = new PrepayRequest();
        request.setMchId("1673424392");
        request.setOutTradeNo(RandomStringUtils.randomNumeric(8));
        request.setTotalFee("0.01");
        request.setBody("QQ公仔");
        request.setNotifyUrl("http://gaga.plus/");

        //创建支付订单
        PrepayResponse response = nativePayApiService.prepay(request);
        log.info("请求参数:{}",request);
        log.info("响应参数:{}",response);
    }
    @Test
    public void test_queryOrderByOutTradeNo() throws Exception {
        QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
        request.setMchId("1673424392");
        request.setOutTradeNo("04805636");
        QueryOrderByOutTradeNoResponse response = nativePayApiService.queryOrderByOutTradeNo(request);
        log.info("请求参数: {}", JSON.toJSONString(request));
        log.info("应答结果: {}", JSON.toJSONString(response));
    }

    @Test
    public void test_refundOrder() throws Exception {
        RefundOrderRequest request = new RefundOrderRequest();
        request.setMchid("1673424392");
        request.setOutTradeNo("66226055");
        request.setOutRefundNo(RandomStringUtils.randomNumeric(8));
        request.setRefundFee("0.01");
        request.setRefundDesc("测试退款");
        request.setNotifyUrl("https://gaga.plus/api");

        Object response = nativePayApiService.refundOrder(request);
        log.info("请求参数: {}", JSON.toJSONString(request));
        log.info("应答结果: {}", JSON.toJSONString(response));
    }

    @Test
    public void test_getRefundOrder() throws Exception {
        GetRefundOrderRequest request = new GetRefundOrderRequest();
        request.setMchId("1673424392");
        request.setOutRefundNo("97173577");

        GetRefundOrderResponse response = nativePayApiService.getRefundOrder(request);
        log.info("请求参数: {}", JSON.toJSONString(request));
        log.info("应答结果: {}", JSON.toJSONString(response));
    }
}
